<template>
	<el-container v-if="isAlready">
		<el-header style="padding-top: 0px;padding-bottom: 0px;height: 60px;border-bottom: 1px solid #e6e6e6;">
			<div class="left-panel">
				<fsButton ref="button" plain size="default" :buttons="buttons" @clickHandler="buttonHandler"></fsButton>
			</div>
		</el-header>
		<el-main style="padding: 0px;background-color: #fff;">
			<el-container>
				<el-header style="width: 98%;height: 60px;padding: 0px 15px;border: 1px rgba(0, 0, 0, .08) solid;
				margin-left: 1%;background-color: rgb(249, 249, 249);">
					<div class="left-panel">
						<div style="margin-left: -5px;float: left;">
							<img class="topblockimg" src="img/pandian-icon.png"/>
						</div>
						<label style="font-size: 14px;width: 70px;margin-top:0px;margin-left: 10px;">盘点处理</label>
					</div>
					<div class="right-panel">
						<ex-search v-if="searches" ref="search" :searches="searches" @searchOk="searchHandler" default-code="assetCode" placeholder="请输入资产编码" :fixedParam=fixedParams></ex-search>
					</div>
				</el-header>
				<el-main class="nopadding">
					<ex-table id="assetTable" ref="table" :columns="columns" :url="this.$API.asset.checkDetailApi.page.url" :refs="this.$refs" search="search" @columnClick="columnClickHandler"></ex-table>
				</el-main>
			</el-container>
		</el-main>
	</el-container>
	<data-card ref="dataCard"></data-card>
	<save-dialog ref="saveDialog" @success="searchHandler"/>
</template>

<script>
import ExTable from "@/components/exTable/index.vue";
import ExSearch from "@/components/exSearch/index.vue";
import ExTreeNavigation from "@/components/exSearchTree/index.vue";
import ExSearchTree from "@/components/exSearchTree/index.vue";
import saveDialog from "@/views/asset/check/checkDetail/view.vue";
import DataCard from "@/views/asset/card/card.vue";
import tool from "@/utils/tool";

export default {
	name: '盘点处理',
	components: {
		DataCard,
		saveDialog,
		ExSearchTree,
		ExTreeNavigation,
		ExSearch,
		ExTable,
	},
	data() {
		return {
			dialog: {
				save: false,
				permission: false,
			},
			isAlready: false,
			assetIds:[],
			checkTaskData:{},
			apiObj: [],
			buttons: [
				{name: "盘点结果提交", icon: "el-icon-edit", click: "checkHandler",type: "info", permission: "checkHandler"},
				{name: "编辑", icon: "el-icon-edit", click: "edits",type: "info", permission: "checkHandlerEdit"},
				{name: "删除", icon: "el-icon-delete", click: "removeDataByDocumentQuery",type: "info", permission: "checkHandlerDel"},
			],
			searches: null,
			columns: [
				// {code: "操作", name: "操作", width: "70",
				// 	click:[
				// 		{label:"编辑",icon:"el-icon-edit",event:'editDetail',type: "info", permission: ""},
				// 	]
				// },
				{code: "attachFiles", name: "拍照信息", width:"90",formatter: "IMAGE"},
				{code: "checkResult", name: "盘点结果", width:"100",formatter:"DICT",key: "CHECK_RESULT"},
				{code: "isPhoto", name: "是否必须拍照", width:"110",formatter:"DICT",key: "IS_PHOTO"},
				{code: "checkUser", name: "盘点人", width:"100",formatter: "DICT",key: "SysUser"},
				{code: "realityCheckUser", name: "实际盘点人", width:"110",formatter: "DICT",key: "SysUser"},
				{code: "checkDate", name: "盘点日期", width:"150",formatter: "DATE"},
				{code: "assetCode", name: "资产编码", width:"160",click:"show1"},
				{code: "ncCode", name: "财务编码", width:"170"},
				{code: "assetCategory", name: "资产类别", width:"210",formatter:"DICT",key: "FamsAssetCategory"},
				{code: "assetName", name: "资产名称", width:"200"},
				{code: "financialCode", name: "财务类型",width: "85", formatter: function(row){
						if(row.ncCode){
							return "账内";
						}
						return "账外";
					}},
				{code: "model", name: "规格型号", width:"120"},
				{code: "originalValue", name: "资产原值", width:"120"},
				{code: "netValue", name: "资产净值", width:"120"},
				{code: "usingUser", name: "使用人", width:"100",formatter: "DICT",key: "SysUser"},
				{code: "usingOrg", name: "使用部门", width:"200",formatter: "DICT",key: "SysOrganization"},
				{code: "realityUsingUser", name: "实际使用人", width:"150",formatter: "DICT",key: "SysUser"},
				{code: "assetPosition", name: "资产位置", width:"100",formatter: "DICT",key: "FamsPosition"},
				{code: "checkPosition", name: "盘点位置", width:"100",formatter: "DICT",key: "FamsPosition"},
				{code: "assetCount", name: "数量", width:"70"},
				{code: "checkAccount", name: "盘点数量", width:"90"},
				{code: "checkDescription", name: "盘点备注说明", width:"300"},
			],
			parentLevelCode:"",
			loginUser: tool.current.user(),
			readDeptIds: this.$TOOL.current.readDeptIds(),
			fixedParams: {
				documentIds: [],
			},
			minioServerUrl: '',
		}
	},
	async created() {
		if (this.$route.params.documentIds && '1c' !== this.$route.params.documentIds) {
			this.fixedParams.queryDocumentIds = this.$route.params.documentIds.split(',');
		}
		this.fixedParams.isManage = '1'
		this.fixedParams.queryUser = this.loginUser.id;
		this.fixedParams.readDeptIds = this.readDeptIds;
		if (this.$route.params.taskId) {
			this.fixedParams.taskId = this.$route.params.taskId;
		}
		let queryDtoDept = {
			firstEntity: "FamsAssetCheckTask",
			firstEntityId: this.fixedParams.taskId,
			secondEntity: "FamsAssetCategory",
		};
		let deptRsp = await this.$API.system.relMap.load.post(queryDtoDept);
		if (deptRsp.code === 2000 && Array.isArray(deptRsp.data)) {
			this.assetCategoryIds = deptRsp.data;
		}
		this.searches = [
			{type: "input", name: "盘点单编码", code: "documentCode", placeholder: "请输入盘点单编码", maxlength: 30, showLimit: true},
			{type: "select", name: "盘点结果", code: "checkResult", placeholder: "请选择盘点结果", dictCode:'CHECK_RESULT'},
			{type: "selector", name: "盘点人", code: "checkUser", placeholder: "请选择盘点人", dictCode:'SysUser',priKey:true},
			{type: "selector", name: "实际盘点人", code: "realityCheckUser", placeholder: "请选择实际盘点人", dictCode:'SysUser',priKey:true},
			{type: "input", name: "资产编码", code: "assetCode", placeholder: "请输入资产编码"},
			{type: "input", name: "财务编码", code: "ncCode", placeholder: "请输入财务编码"},
			{type: "input", name: "资产名称", code: "assetName", placeholder: "请输入资产名称"},
			{type: "tree-select", name: "资产类别", code: "assetCategoryIds", placeholder: "请选择资产类别", dictCode:'FamsAssetCategory', checkStrictly:true,multiple:true,dataPermissionProcess:true,permissionData:this.assetCategoryIds},
			{type: "select", name: "财务类型", code: "assetType", placeholder: "请选择财务类型", dictCode:'FINANCIAL_TYPE'},
			{type: "tree-select", name: "资产位置", code: "assetPositionIds", placeholder: "请选择资产位置", dictCode:'FamsPosition', checkStrictly:true,multiple:true},
			{type: "select", name: "是否拍照", code: "isPhoto", placeholder: "请选择是否拍照", dictCode:'IS_PHOTO'},
		]
		this.isAlready = true;
	},
	beforeCreate(){
	},
	async mounted() {
		this.minioServerUrl = await this.$TOOL.minioServerUrl();
	},
	methods: {
		buttonHandler(index, name) {
			this.$options.methods[name](this, index);
		},
		async searchTreeHandler(data) {
			let queryParams = {pathCode:data.levelCode}
			this.parentLevelCode= data.levelCode;
			await this.$refs.table.refresh(queryParams);
		},

		async searchHandler() {
			let queryParams = {pathCode: this.parentLevelCode}
			await this.$refs.table.refresh(queryParams);
		},
		columnClickHandler(name,data){
			this.$options.methods[name](this, data);
		},
		//编辑
		edits(_this) {
			let row = _this.$refs.table.selectionOne();
			if (!row) {
				return;
			}
			_this.$nextTick(() => {
				_this.$refs.saveDialog.open(row);
			})
		},
		//编辑
		editDetail(_this,row) {
			_this.$nextTick(() => {
				_this.$refs.saveDialog.open(row);
			})
		},
		async checkHandler(_this, index) {
			let selections = _this.$refs.table.selections();
			if (selections.length === 0) {
				let confirm = await _this.$confirm('确定处理所有项吗？', '警告', {type: 'warning'}).then(() => {
					return true;
				}).catch(() => {
					return false;
				});
				if (!confirm) {
					return;
				}
				_this.$refs.button.loading(index, true);
				let queryParams = {}
				Object.assign(queryParams, _this.$refs.table.queryParams())
				let res = await _this.$API.asset.checkDetailApi.checkHandleDetailByQuery.post(queryParams);
				if (res.code === 2000) {
					_this.$message.success(_this.$t("code." + res.code));
					_this.searchHandler();
				}
				_this.$refs.button.loading(index, false);
			} else {
				_this.$confirm(`确定处理选中的 ${selections.length} 项吗？`, '提示', {
					type: 'warning'
				}).then(async () => {
					_this.$refs.button.loading(index, true);
					let delarray = [];
					for (let index = 0; index < selections.length; index++) {
						const element = selections[index];
						delarray.push(element.id);
					}
					let queryParams = {
						ids: delarray
					}
					let res = await _this.$API.asset.checkDetailApi.checkHandleDetailByQuery.post(queryParams);
					if (res.code === 2000) {
						_this.$message.success(_this.$t("code." + res.code));
						_this.searchHandler();
					}
					_this.$refs.button.loading(index, false);
				}).catch(() => {
					_this.$refs.button.loading(index, false);
				});
			}

		},
		//查看
		async show1(_this, row,index) {
			let assetCategoryData = await _this.$cache.data.loadAssetCategory(row.assetCategory);
			try {
				_this.loading = true;
				let entity = {cardId: assetCategoryData.assetCardId, _editFlag: false}
				_this.$nextTick(async () => {
					let assetRsp = await _this.$API.asset.asset.findById.post({id: row.assetId});
					if (assetRsp.code === 2000 && assetRsp.data) {
						_this.$refs.button.loading(index, false);
						_this.$refs.dataCard.open(Object.assign(entity, assetRsp.data));
					} else {
						_this.$refs.button.loading(index, false);
						_this.$message.message("未查询到资产信息");
					}
				});
			} finally {
				_this.loading = false;
			}
		},
		//批量删除
		del(_this, index) {
			let selections = _this.$refs.table.selections();
			if (selections.length === 0) {
				_this.$message.warning(_this.$t("common.please_select_data"));
				return;
			}
			_this.$confirm(`确定删除选中的 ${selections.length} 盘点明细吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				_this.$refs.button.loading(index, true);
				let delarray = [];
				for (let index = 0; index < selections.length; index++) {
					const element = selections[index];
					delarray.push(element.id);
				}
				let res = await _this.$API.asset.checkDetailApi.deleteByIds.post(delarray);
				if (res.code === 2000) {
					_this.$message.success(_this.$t("code."+res.code));
					_this.searchHandler();
					_this.$store.dispatch("commit",selections[0].entityName);
				}
				_this.$refs.button.loading(index, false);
			}).catch(() => {
				_this.$refs.button.loading(index, false);
			});
		},
		//批量删除
		async removeDataByDocumentQuery(_this, index) {
			let selections = _this.$refs.table.selections();
			if (selections.length === 0) {
				_this.$confirm('确定删除所有项吗？', '警告', {type: 'warning'}).then(() => {
					_this.$refs.button.loading(index, true);
					let queryParams = {}
					Object.assign(queryParams, _this.$refs.table.queryParams())
					let res = _this.$API.asset.checkDetailApi.removeDataByDocumentQuery.post(queryParams);
					if (res.code === 2000) {
						_this.$message.success(_this.$t("code." + res.code));
						_this.searchHandler();
					}
					_this.$refs.button.loading(index, false);
				}).catch(() => {
					_this.$refs.button.loading(index, false);
				});
			} else {
				_this.$confirm(`确定删除选中的 ${selections.length} 项吗？`, '提示', {
					type: 'warning'
				}).then(async () => {
					_this.$refs.button.loading(index, true);
					let delarray = [];
					for (let index = 0; index < selections.length; index++) {
						const element = selections[index];
						delarray.push(element.id);
					}
					let res = await _this.$API.asset.checkDetailApi.deleteByIds.post(delarray);
					if (res.code === 2000) {
						_this.$message.success(_this.$t("code." + res.code));
						_this.searchHandler();
					}
					_this.$refs.button.loading(index, false);
				}).catch(() => {
					_this.$refs.button.loading(index, false);
				});
			}

		},

	}
}

</script>
<style scoped>

</style>
